package test.mp.domain.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import test.mp.domain.po.User;

/**
 * @ClassName PageQuery
 * @Description 分页查询
 * @Author wxx
 * @Date 2024/08/25/22:00
 * @Version 1.0
 */
@Data
@ApiModel(description = "分页查询实体")
public class PageQuery {
    @ApiModelProperty("页码")
    private Integer pageNo = 1;
    @ApiModelProperty("数量")
    private Integer pageSize = 5;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc = true;


    /**
     * 转换为mybatis-plus分页对象
     * @param itmes
     * @return
     * @param <T>
     */
    public <T> Page<T> toMpPage(OrderItem ... itmes){
        Page<T> page = Page.of(pageNo, pageSize);
        // 1.2 排序条件
        if(StrUtil.isNotBlank(sortBy)){
            // 不为空
            page.addOrder(new OrderItem(sortBy, isAsc));
        }else if(itmes != null){
            // 为空，默认排序
            page.addOrder(itmes);
        }
        return page;
    }


    public <T> Page<T> toMapPage(String defaultSortBy,Boolean defaultIsAsc){
        return toMpPage(new OrderItem(defaultSortBy, defaultIsAsc));
    }

    public <T> Page<T> toMapPageDefaultSortByCreateTime(){
        return toMpPage(new OrderItem("create_time", false));
    }

    public <T> Page<T> toMapPageDefaultSortByUpdateTime(){
        return toMpPage(new OrderItem("update_time", false));
    }

}
